<template>
  <el-dialog
    v-dialogDrag
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="120px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="用户编号" prop="userId">
            <el-input v-model="dataForm.userId" placeholder="用户编号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="登录名" prop="loginName">
            <el-input v-model="dataForm.loginName" placeholder="登录名"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="姓名" prop="userName">
            <el-input v-model="dataForm.userName" placeholder="姓名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" prop="gender">
            <el-select v-model="dataForm.gender" placeholder="请选择性别" style="width: 100%">
              <template v-for="(item, index) in dict.Gender">
                <el-option :key="index" :label="item.label" :value="item.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="邮箱" prop="userEmail">
            <el-input v-model="dataForm.userEmail" placeholder="邮箱"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电话" prop="userTelnum">
            <el-input v-model="dataForm.userTelnum" placeholder="电话"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="描述" prop="userDesc">
        <el-input v-model="dataForm.userDesc" placeholder="描述"></el-input>
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="失效时间" prop="endTime">
            <el-date-picker
              v-model="dataForm.endTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="选择日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="启用状态" prop="status">
            <el-select v-model="dataForm.status" placeholder="请选择状态" style="width: 100%">
              <template v-for="(item, index) in dict.UserStatus">
                <el-option :key="index" :label="item.label" :value="item.value"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,

        dataForm: {
          id: 0,
          userId: '',
          loginName: '',
          userName: '',
          gender: '',
          userEmail: '',
          userTelnum: '',
          userDesc: '',
          endTime: null,
          status: '',
        },
        dataRule: {
          userId: [
            {required: true, message: '用户编号不能为空', trigger: 'blur'}
          ],
          loginName: [
            {required: true, message: '登录名不能为空', trigger: 'blur'}
          ],
          userName: [
            {required: true, message: '姓名不能为空', trigger: 'blur'}
          ],
          gender: [
            {required: true, message: '性别不能为空', trigger: 'blur'}
          ],
          userEmail: [
            {message: '邮箱不能为空', trigger: 'blur'}
          ],
          userTelnum: [
            {required: true, message: '电话不能为空', trigger: 'blur'}
          ],
          userDesc: [
            {message: '描述不能为空', trigger: 'blur'}
          ],
          createTime: [
            {message: '创建时间不能为空', trigger: 'blur'}
          ],
          endTime: [
            {message: '失效时间不能为空', trigger: 'blur'}
          ],
          status: [
            {required: true, message: '启用状态不能为空', trigger: 'blur'}
          ],
          version: [
            {message: '版本号不能为空', trigger: 'blur'}
          ],
          timeStamp: [
            {message: '时间戳不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init(id, row) {
        this.dataForm.id = id
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id && this.dataForm.id > 0) {
            this.dataForm = row
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/api/user/${!this.dataForm.id ? 'add' : 'mod'}`),
              method: 'post',
              data: this.$http.adornData(this.dataForm),
            }).then(({data}) => {
              if (data && data.code === '000000') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1000,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else{
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
